package com.intermancer.predictor.mutation;

import java.util.ArrayList;
import java.util.List;

import com.intermancer.predictor.gene.Chromosome;
import com.intermancer.predictor.gene.GeneticUtility;

public class ChromosomeRegistrar implements ChromosomeFactory {
	
	private List<Chromosome> registry;
	
	public ChromosomeRegistrar() {
		registry = new ArrayList<Chromosome>();
	}

	@Override
	public Chromosome getChromosome() {
		int index = MutationUtility.getDiceroll(registry.size());
		return GeneticUtility.cloneChromosome(registry.get(index - 1));
	}

	@Override
	public List<Chromosome> getChromosomes() {
		List<Chromosome> copy = new ArrayList<Chromosome>(registry.size());
		for(Chromosome chromosome : registry) {
			copy.add(GeneticUtility.cloneChromosome(chromosome));
		}
		return copy;
	}
	
	public void addChromosomes(List<Chromosome> chromosomes) {
		if(chromosomes != null) {
			for(Chromosome chromosome : chromosomes) {
				if(!registry.contains(chromosome)) {
					registry.add(GeneticUtility.cloneChromosome(chromosome));
				}
			}
		}
	}
	
	public void addChromosomes(ChromosomeFactory factory) {
		if(factory != null) {
			addChromosomes(factory.getChromosomes());
		}
	}

}
